<?php
require_once("../../../config.inc.php");
require_once(SERVER_FS_ROOT."/app/_inc/include.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");
require_once("../_inc/procFunction.php");

$errMsg = sxREQUEST('msg');

if (sxReq('reqtype')=='chgMod') {
	$objUser->aSV['layoutList']['fa'] = '-all';
	$_REQUEST['fa'] = '-all';
	$objUser->aSV['layoutList']['sub'] = '-all';
	$_REQUEST['sub'] = '-all';
}
if (sxReq('reqtype')=='chgFa') {
	$objUser->aSV['layoutList']['sub'] = '-all';
	$_REQUEST['sub'] = '-all';
}

$sSQL = "SELECT sub_module FROM sub_module";
$objSDAO->query($sSQL, "pu");
if(!$row = $objSDAO->fetch("pu")) {
	$url = "../modl/moduleList.php?xsn=".XSN.
		"&msg=".urlencode(_t("no sub-module defined")); 
	serverRedir($url);
}

$sSQL = "SELECT m.module, m.description FROM  module m ORDER BY m.module";
$objSDAO->query($sSQL, "pu");
$aModZ = array();
while($row = $objSDAO->fetch("pu")) {
	$aModZ[$row['module']] = $row['module']." - ".$row['description'];
}
reset($aModZ);
$module = sxREQUEST('mod');
if($module==''){
	$module = sxVx($objUser->aSV,'layoutList','mod');
	if($module==''){
		$module = key($aModZ);
	}
} 
$_REQUEST['mod'] = $module;
$objUser->aSV['layoutList']['mod'] = $module;

$sSQL = "SELECT f.functional_area, f.description FROM functional_area f"
	." WHERE f.module = '$module' ORDER BY f.functional_area";
$objSDAO->query($sSQL, "pu");
$aFaZ = array();
while($row = $objSDAO->fetch("pu")) {
	$aFaZ[$row['functional_area']] = $row['functional_area']." - ".$row['description'];
}
$aFaZ['-all'] = " -- all functional-areas -- ";
reset($aFaZ);
$functionalArea = sxREQUEST('fa');
if ($functionalArea=='') {
	$functionalArea = sxVx($objUser->aSV,'layoutList','fa');
	if($functionalArea==''){
		$functionalArea = '-all';
	}
} 
$_REQUEST['fa'] = $functionalArea;
$objUser->aSV['layoutList']['fa'] = $functionalArea;

$sSQL = "SELECT s.sub_module, s.description FROM sub_module s "
	." WHERE s.module = '$module' ";
if ($functionalArea == '-all') {
	$_REQUEST['sub'] == '-all';
} else {
	$sSQL .= " AND s.functional_area = '$functionalArea' ";
}
$sSQL .= " ORDER BY s.sub_module";
$objSDAO->query($sSQL, "pu");
$aSubZ = array();
while($row = $objSDAO->fetch("pu")) {
	$aSubZ[$row['sub_module']] = $row['sub_module']." - ".$row['description'];
	$aFaSubZ[$row['sub_module']] = $row['functional_area'];
}
$aSubZ['-all'] = " -- all sub-modules -- ";
reset($aSubZ);
$subModule = sxREQUEST('sub');
if($subModule==''){
	$subModule = sxVx($objUser->aSV,'layoutList','sub');
	if($subModule==''){
		$subModule = '-all';
	}
}
$_REQUEST['sub'] = $subModule;
$objUser->aSV['layoutList']['sub'] = $subModule;

$objUser->setHistoryVar(array('mod'=>$module,'fa'=>$functionalArea,'sub'=>$subModule));

if(sxREQUEST("reqtype") == "exp"){
	$errMsg .= genExp();		
}

$sSQL = "SELECT pu.*, m.description AS descr_module, sm.description AS descr_sub_module,
			sm.functional_area, fa.description AS fades
		FROM sub_module sm
		INNER JOIN module m ON m.module = sm.module
		INNER JOIN functional_area fa ON fa.functional_area = sm.functional_area
		LEFT OUTER JOIN process_unit pu ON pu.sub_module = sm.sub_module AND pu.module = sm.module
		WHERE sm.module='$module' ";
if ($subModule != '-all') {
	$sSQL .= " AND sm.sub_module='$subModule'";
}
$sSQL .= " ORDER BY process_unit";

$objSDAO->query($sSQL, "pu");
$row = $objSDAO->fetch("pu");
$descrModule = $row["descr_module"];
$descrSubModule = $row["descr_sub_module"];
//output HTML
$aOpz = array("kMenu"=>'pro.pru');
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	var aElemObbl = Array();
	var aDescrElemObbl = Array();
	function genExp() {
		document.objFrm.reqtype.value = 'exp';
		document.objFrm.submit();
	}
	function submCM() {
		document.objFrm.reqtype.value = 'chgMod';
		document.objFrm.submit();
	}
	function submCF() {
		document.objFrm.reqtype.value = 'chgFa';
		document.objFrm.submit();
	}
	function submCS() {
		document.objFrm.reqtype.value = 'chgSub';
		document.objFrm.submit();
	}
	function selezExp() {
		for(k=1;k<1000;k++) {
			oExp = document.getElementById('exp_'+String(k));
			if (!oExp) break;
			oExp.checked = true;
		}
	}
	function deSelezExp() {
		for(k=1;k<1000;k++) {
			oExp = document.getElementById('exp_'+String(k));
			if (!oExp) break;
			oExp.checked = false;
		}
	}
</script>
<?php
echo(bodyHTML($aOpz));
echo("<form name=\"objFrm\" id=\"objFrm\" action='".$_SERVER['PHP_SELF']."' method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n<tr>\n<td>\n");
$aOptCM = array('onChange'=>'submCM();');
$aOptCF = array('onChange'=>'submCF();');
$aOptCS = array('onChange'=>'submCS();');
echo("<tr>" .
		"<td class=\"labl01\" width='20%'>"
			."<a class=\"labl01\" href=\"../modl/moduleView.php?mod=$module&xsn=".XSN."\">Module</a></td>"
			."<td class='text01' width='80%'>".getHtmlSelect('mod',$aModZ,'@REQ',$aOptCM)."</td></tr>");
echo("<tr>" .
		"<td class=\"labl01\">"
			."<a class=\"labl01\" href=\"../modl/functionalAreaView.php?mod=$module&fa=$functionalArea&xsn=".XSN."\">Functiona-Area</a></td>"
			."<td class='text01'>".getHtmlSelect('fa',$aFaZ,'@REQ',$aOptCF)."</td></tr>");
echo("<tr>" .
		"<td class=\"labl01\">"
			."<a class=\"labl01\" href=\"../modl/subModView.php?mod=$module&sub=$subModule&xsn=".XSN."\">Sub-module</a></td>"
			."<td class='text01'>".getHtmlSelect('sub',$aSubZ,'@REQ',$aOptCS)."</td></tr>");
echo("</table>\n");

$aCmd["sel"] = array("pag"=>"javascript:selezExp()",
					"title"=>_t("select all"),
					"text"=>_t("select all"),'align'=>'L'
					);
$aCmd["des"] = array("pag"=>"javascript:deSelezExp()",
					"title"=>_t("unselect all"),
					"text"=>_t("unselect all"),'align'=>'L'
					);
$aCmd["exp"] = array("pag"=>"javascript:genExp();",
										"title"=>_t("export"),
										"text"=>_t("export"),
										"perm"=>"newmod",'align'=>'L');
$aCmd["ins"] = array("pag"=>"puIns.php?mod=$module&subMod=$subModule",
										"title"=>_t("new process-unit"),
										"text"=>_t("new process-unit"),
										"perm"=>"newmod");
$aCmd["back"] = array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1),
										"perm"=>"");
$sCmd = (commandsHTML($aCmd));	
echo $sCmd;	

echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\" colspan=\"5\">"._t("List of Process-units")."</td>" .
	"</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">"._t('Export')."</td>" .
		"<td class=\"tabh01\" width=\"14%\">"._t('Name')."</td>" .
		"<td class=\"tabh01\" width=\"54%\">"._t('Description')."</td>" .
		"<td class=\"tabh01\" width=\"20%\">"._t('Type')."</td>" .
	"</tr>\n");	
$numElem = 0;
while($row) {
	if($row['process_unit']!='') {
		$numElem ++;
		echo("<tr>" .
				"<td class=\"text01\" valign='top' align=\"center\">" .
				"<input type=\"checkbox\" name=\"exp_".$numElem."\" id=\"exp_".$numElem.
				"\" value=\"".$row["process_unit"]."\" class='text01'></td>" .
				"<td class=\"text01\" valign='top'><a class=\"text01\" ".
					goToPag("puView.php?pu=".$row["process_unit"]."&xsn=".XSN).
					" title=\""._t("Visualizza processo")." '".sxHE($row["description"])."'\">".
					$row["process_unit"]."</a></td>\n" .
				"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
				"<td class=\"text01\" valign='top'>".sxHE($row["type"])."</td>" .
			"</tr>\n");
	}
	$row = $objSDAO->fetch("pu");
}
echo("</table>\n");
echo $sCmd;	
echo("</td>\n</tr>\n</table>\n");
echo("</form>");
echo(footerHTML());
exit();

function genExp() {
	global $objUser;
	$aReqExp = array();
 	foreach($_REQUEST as $key=>$value) {
 		if(substr($key,0,4) == "exp_") {
  			$aReqExp[] = array('process_unit',$value);
  			$objUser->setHistoryVar(array($key=>''));
 		}
 	}
  	$objUser->setHistoryVar(array('reqtype'=>''));
	if (count($aReqExp) == 0) {
		return _t("Nessun elemento selezionato");
	}
	$errMsg = tabExport($aReqExp);
	return $errMsg;
}
